home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / mac / PageMaker 6.5 SDK Mac / SourceCode / UtilityCode / PageMakerMemory.cpp < prev    next >
C/C++ Source or Header  |  1996-09-04  |  4KB  |  132 lines

  1. /*
  2.  *--- PageMakerMemory.c --------------------------------------------------------
  3.  *    Copyright (C) 1988-95 Adobe Systems, Inc.  All rights reserved.
  4.  *
  5.  *    This file contains the PageMaker memory manager interface for both the
  6.  *    Mac OS and Windows 95 environments.  These are wrapper functions that
  7.  *  you can use in your PageMaker plug-in source code.
  8.  *
  9.  *    Multiple Locks -
  10.  *        Locks do not stack.  Locking the same block more than one time yields
  11.  *        results that are not consistent across all environments.  Don't do
  12.  *        it.  Under Windows, a block that is locked twice then unlocked once
  13.  *        is still locked -- on the Mac it is left unlocked.
  14.  *------------------------------------------------------------------------------
  15.  */
  16.  
  17. #include <stdlib.h>            // For calloc() & free() prototypes
  18. #ifdef MACINTOSH
  19.     #include <Memory.h>
  20. #endif //MACINTOSH
  21. #ifdef WINDOWS
  22.     #include <Windows.h>   // Windows header file provides it's own double include protection 
  23. #endif    //MACINTOSH
  24.  
  25. #include "PMMemory.h"
  26.  
  27. /*
  28.  *--- MMAlloc ------------------------------------------------------------------
  29.  *    Allocate a block from the global heap.  The new block is relocatable
  30.  *    (must be locked before it can be used) and non-purgable.  Handles to
  31.  *    blocks are valid only to the application that allocated them and cannot 
  32.  *    be passed from one application to another.
  33.  *    Depending on the environment, the actual number of bytes allocated may
  34.  *    be slightly larger than the requested.
  35.  *
  36.  *    SIDE EFFECTS:
  37.  *    Memory is allocated from the global heap.  It should later be freed with 
  38.  *    a call to MMFree().
  39.  *------------------------------------------------------------------------------
  40.  */
  41. PMHandle MMAlloc(size_t sz)
  42. {
  43. #if    WINDOWS
  44.     return (PMHandle) GlobalAlloc(GMEM_MOVEABLE, (unsigned long) sz);
  45. #else
  46.     return NewHandleClear(sz);
  47. #endif
  48. }
  49.  
  50.  
  51. /*
  52.  *--- MMFree -------------------------------------------------------------------
  53.  *    Free a block allocated by MMAlloc().
  54.  *------------------------------------------------------------------------------
  55.  */
  56. void MMFree(PMHandle h)
  57. {
  58. #if    WINDOWS
  59.     GlobalFree(h);
  60. #else
  61.     DisposHandle(h);
  62. #endif
  63. }
  64. /*
  65.  *--- MMLock -------------------------------------------------------------------
  66.  *    Lock the memory block referenced by a handle.  The handle must 
  67.  *    have been allocated by MMAlloc().
  68.  *    Memory should later be unlocked with MMUnlock().
  69.  *------------------------------------------------------------------------------
  70.  */
  71.  
  72. void * MMLock(PMHandle h)
  73. {
  74. #if    WINDOWS
  75.     //    GlobalLock will fail if the block was purged.
  76.     return (void *) GlobalLock(h);
  77. #else
  78.     HLockHi(h);
  79.     return (void *)(*h);
  80. #endif
  81. }
  82.  
  83. /*
  84.  *--- MMUnlock -----------------------------------------------------------------
  85.  *    Unlock a block of memory that was locked by MMUnlock().
  86.  *    The block of memory in the global heap is made moveable.
  87.  *------------------------------------------------------------------------------
  88.  */
  89. void MMUnlock(PMHandle h)
  90. {
  91. #if    WINDOWS
  92.     GlobalUnlock(h);
  93. #else
  94.     HUnlock(h);
  95. #endif
  96. }
  97.  
  98. /*
  99.  *--- MMIsLocked ---------------------------------------------------------------
  100.  *    Determine whether the memory referenced by the handle is locked
  101.  *    or unlocked.  The handle must be be a legal handle (not NULL, 
  102.  *    not garbage).  Errors are not reported, but always result in a
  103.  *    return of false.
  104.  *------------------------------------------------------------------------------
  105.  */
  106. PMBool MMIsLocked(PMHandle h)
  107. {
  108. #if    WINDOWS
  109.     return    ((GlobalFlags(h) & GMEM_LOCKCOUNT) > 0) ? true : false;
  110. #else
  111.     char ch = HGetState(h);
  112.     return (ch & 0x80 ? true : false);
  113. #endif
  114. }
  115.  
  116. /*
  117.  *--- MMResizeHandle -----------------------------------------------------------
  118.  *        Changes size of an unlocked memory block to new specified size.
  119.  *------------------------------------------------------------------------------
  120.  */
  121. PMErr MMResizeHandle(PMHandle * h, size_t sz)
  122. {
  123. #if WINDOWS
  124.     return (PMErr) GlobalReAlloc(h, sz, GMEM_MOVEABLE);
  125. #else
  126.     SetHandleSize(*h, sz);
  127.     return -MemError();
  128. #endif
  129. }
  130.  
  131. // end of PageMakerMemory.c
  132.